iT邦幫忙

2023 iThome 鐵人賽

DAY 20
0
Software Development

LELECOCODE 每一天系列 第 20

Day 20: Leetcode 小挑戰,30 Days of JavaScript

  • 分享至 

  • xImage
  •  

Day 20: Is Object Empty

Given an object or an array, return if it is empty.

An empty object contains no key-value pairs.
An empty array contains no elements.
You may assume the object or array is the output of JSON.parse.

var isEmpty = function(obj) {
    
};

Example 1:
Input: obj = {"x": 5, "y": 42}
Output: false
Explanation: The object has 2 key-value pairs so it is not empty.

Example 2:
Input: obj = {}
Output: true
Explanation: The object doesn't have any key-value pairs so it is empty.

Example 3:
Input: obj = [null, false, 0]
Output: false
Explanation: The array has 3 elements so it is not empty.


var isEmpty = function(obj) {
    for (let key in obj) 
        return false;
    return true;
};

新學到的解法:

var isEmpty = function(obj) {
    for (const _ in obj) return false;
    return true;
};

上一篇
Day 19: Leetcode 小挑戰,30 Days of JavaScript
下一篇
Day 21: Leetcode 小挑戰,30 Days of JavaScript
系列文
LELECOCODE 每一天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言